'''
Created on Feb 14, 2010

@author: Roni
'''
import unittest
from tests.cliqueSearchAbstractTest import CliqueSearchAbstractTest
import graph
from cliques.closest.closestCliqueSearch import ClosestCliqueSearch
import cliques.utils

class unittest_closestCliqueSearch(CliqueSearchAbstractTest):


    def setUp(self):
        pass


    def tearDown(self):
        pass


    def testCheckFindingClosest(self):
        test_graph = graph.graph()
        nodes = [0,1,2,3,4,5,6,7,8]
        test_graph.add_nodes(nodes)
        test_graph.add_edge(0,1)
        test_graph.add_edge(0,2)
        test_graph.add_edge(0,3)
        test_graph.add_edge(0,4)
        test_graph.add_edge(0,5)
                
        # A 3-clique 0,1,5
        test_graph.add_edge(1,5)
        
        # A 4-clique 0,2,3,4
        test_graph.add_edge(3,4)
        test_graph.add_edge(2,4)
        test_graph.add_edge(2,3)
        
        # A 5-clique that is far 1,5,6,7,8
        test_graph.add_edge(1,6)
        test_graph.add_edge(1,7)
        test_graph.add_edge(1,8)
        test_graph.add_edge(5,6)
        test_graph.add_edge(5,7)
        test_graph.add_edge(5,8)
        test_graph.add_edge(6,7)
        test_graph.add_edge(6,8)
        test_graph.add_edge(7,8)
        
        # Run the search for 4 clique
        searcher = ClosestCliqueSearch()
        expected = [0,2,3,4]
        start = 0
        k = 4
        searcher.run(test_graph,k,start)
        self.assertTrue(searcher.done)
        found_clique = searcher.output_clique()        
        self.assertFalse(found_clique==None)
        self.assertTrue(cliques.utils.list_set_equals(found_clique, expected))
        
        # Run the search for a 5 clique
        searcher = ClosestCliqueSearch()
        expected = [1,5,6,7,8]
        start = 0
        k = 5
        searcher.run(test_graph,k,start)
        self.assertTrue(searcher.done)
        found_clique = searcher.output_clique()        
        self.assertFalse(found_clique==None)
        self.assertTrue(cliques.utils.list_set_equals(found_clique, expected))

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()